package com.example.appbarlayoutdemo.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author: njb
 * @date: 2020/3/24 0024 11:00
 * @desc:
 */
public abstract class BaseActivity extends AppCompatActivity {
    public Context context;
    public Toast toast;
    protected Unbinder unbinder;


    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void addListener();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        unbinder = ButterKnife.bind(this);
        context = this;
        initView();
        addListener();
    }

    /**
     * 打开Activity
     *
     * @param cls
     */
    public void startA(@NonNull Class<?> cls) {
        if (cls == null) {
            return;
        }

        Intent intent = new Intent(context, cls);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (unbinder != null) {
            unbinder.unbind();
        }

    }

    /**
     * @param s
     */
    public void showToast(@NonNull String s) {
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }

    public void showFileDialog() {
    }

    public void hideFileDialog() {
    }


    /**
     * 通过资源res获得view
     *
     * @param res
     * @return
     */
    public View getViewByRes(@LayoutRes int res) {
        return LayoutInflater.from(context).inflate(res, null);
    }

    /**
     * 获得TextView 的文本
     *
     * @param tv
     * @return
     */
    public String getTV(TextView tv) {
        return tv == null ? "" : tv.getText().toString().trim();
    }

}
